import { Window_Selectable } from "@/windows/Window_Selectable.js";

export class Window_EquipItem extends Window_Selectable {
    constructor(width,height) {
        super(width, height,false);
        this.init(width,height);
        
    }

    init(width,height){
        this._actor = null;
        
        this._etypeId=0;
        this.numWidth=0;
        this.okHandler=null;
        this.cancelHandler=null;
        this.changedHandler=null;
        this.unload={trydown:true}
        this._list=[this.unload];
        super.init(width,height);
    }

    destroy(opts){
        super.destroy(opts);
        this.okHandler=null;
        this.cancelHandler=null;
        this.changedHandler=null;
    }

    setActor(actor) {
        if (this._actor !== actor) {
            this._actor = actor;
            this.refresh();
        }
    }

    setETypeId(etypeId){
        if(this._etypeId!=etypeId){
            this._etypeId=etypeId;
            this.refresh();
        }
    }

    setHandler(name,callback){
        if(name=='ok'){
            this.okHandler=callback;
        }else if(name=='cancel'){
            this.cancelHandler=callback;
        }else if(name=='changed'){
            this.changedHandler=callback;
        }
    }

    column(){return 2;}
    viewColumn(){return 2;}
    row(){return this.length()-1;}
    viewRow(){return Math.floor(this.contentsHeight()/this.itemHeight());}
    length(){return this._list.length;}
    itemWidth(){return this.contentsWidth()/this.viewColumn();}

    itemEnabled(index) {
        let item=this._list[index];
        return item.trydown||(super.itemEnabled(index)&&this._actor?.canEquip(item));
    }

    makeItemList(){
        if(this._actor){
            if(!this._etypeId){
                this._list.length=0;
            }else{
                this._list=$gameParty.equipItems().filter(item=>item?.etypeId==this._etypeId);
            }
        }else{
            this._list.length=0;
        }
        this._list.push(this.unload);
    }

    drawItem(index,x,y,width,height) {
        let item = this._list[index];
        if (item&&!item.trydown) {
            let nwidth=this.numberWidth();
            let enabled=this.itemEnabled(index);
            if(!enabled){
                this.changePaintOpacity(false);
            }
            this.drawItemName(item, x , y,width-nwidth);
            this.drawText($gameParty.numItems(item), x+width-nwidth, y, nwidth,height);
            if(!enabled){
                this.changePaintOpacity(true);
            }
        }
    }

    numberWidth() {
        if(!this.numWidth){
            this.numWidth=this.textWidth('000');
        }
        return this.numWidth;
    }

    item(index){
        if(index<this.length()-1){
            return this._list[index];
        }
        return null;
    }

    refresh(){
        this.makeItemList();
        super.refresh();
    }

    onResponse(index){
        let item=this.item(index);
        this.okHandler?.(item,index);
        if(index==this.length()-1||(item&&!$gameParty.numItems(item))){
            this.refresh();
        }else{
            this.refreshItem(index);
        }
    }

    onSelectChanged(index,oldIndex){
        this.changedHandler?.(this.item(index),index,this.item(oldIndex),oldIndex);
    }

    onCancel(){
        this.cancelHandler?.();
    }
}